package com.bounds.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.bounds.entity.Person;
import com.bounds.sysmgnt.util.Constants;
import com.bounds.sysmgnt.util.DateUtil;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport implements SessionAware,
		ServletRequestAware {

	protected Map<String, Object> session;
	protected HttpServletRequest request;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Person getSessionUser() {
		Person person = null;
		Object o = request.getSession().getAttribute(Constants.SESSION_USER_KEY);
		if (o != null) {
			person = (Person) o;
			return person;
		}
		o = session.get(Constants.SESSION_USER_KEY);
		if (o != null) {
			person = (Person) o;
			request.getSession().setAttribute(Constants.SESSION_USER_KEY, person);
			return person;
		}
		return person;
	}

	/**
	 * ��ҳ
	 */
	private int pageNo;

	private int pageSize;

	public int getPageNo() {
		if (pageNo < 1) {
			pageNo = 1;
		}
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		if (pageSize < 1) {
			pageSize = 10;
		}
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * ��ȡ��Ŀ·�� ���磺http://127.0.0.1:8080/prj
	 * 
	 * @return
	 */
	public String getBasePath() {
		return request.getScheme() + "://" + request.getServerName() + ":"
				+ request.getServerPort() + request.getContextPath() + "/";
	}

	/**
	 * ��ȡ������ϵͳʱ��
	 * 
	 * @return
	 */
	public String getSysDate() {
		return DateUtil.getDateTime();
	}

}
